/**
 * 使用场景：遍历每个工作表，添加或修改单元格
 */

const ExcelJS = require('exceljs');

async function readExcelData() {
  // 创建一个新的工作簿实例
  let workbook = new ExcelJS.Workbook();

  // 读取现有的Excel文件
  await workbook.xlsx.readFile('./file/物料产品树形图-子产品.xlsx');

  // 遍历每个工作表
  workbook.eachSheet(function(worksheet, sheetId) {
    console.log(`Worksheet ${sheetId}: ${worksheet.name}`);

    // 遍历每一行
    worksheet.eachRow({ includeEmpty: false }, (row, rowNumber) => {
      console.log('000000000');
      // 遍历每一列
      row.eachCell({ includeEmpty: false }, (cell, colNumber) => {
        console.log('111111111');
        // 输出单元格的地址和值
        console.log(`Cell ${cell.address} = ${cell.value}`);
      });
    });

    //添加或修改单元格
    worksheet.getCell('G2').value = "WORLD";
  });

  workbook.xlsx.writeFile('./file/物料产品树形图-子产品3.xlsx');
}

readExcelData().catch(err => console.error(err));
